package com.cn.aam.checaiduo.ui.me;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.base.adapter.decoration.GridSpacingItemDecoration;
import com.cn.aam.checaiduo.ui.me.adapter.ChooseTimeAdapter;
import com.cn.aam.checaiduo.utils.CalendarUtil;
import com.cn.aam.checaiduo.utils.WindowsUtils;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * monker on 2017/5/31.
 */

public class ChooseDateActivity extends BaseActivity implements OnDateSetListener {
    @BindView(R.id.time_tv_from)
    TextView timeTvFrom;

    @BindView(R.id.time_tv_to)
    TextView timeTvTo;

    @BindView(R.id.time_recycler)
    RecyclerView timeRecycler;

    private TimePickerDialog mDialogAll;

    private final int TYPE_FROM = 1;
    private final int TYPE_TO = 2;
    private int curType;
    private String dateFrom, dateEnd;
    private List<String> dates;

    @Override
    protected int layoutId() {
        return R.layout.activity_choose_date;
    }

    public static Intent newInstance(Context context) {
        Intent intent = new Intent(context, ChooseDateActivity.class);
        return intent;
    }

    @Override
    protected void initView() {
        ToolBarManager.with(this, WMContent).setTitle("筛选").setNavigationIcon(R.drawable.ic_nav_arrow_white);
        dates = new ArrayList<>();
        long tenYears = 10L * 365 * 1000 * 60 * 60 * 24L;
        mDialogAll = new TimePickerDialog.Builder()
                .setCallBack(this)
                .setCancelStringId("取消")
                .setSureStringId("确定")
                .setTitleStringId("选择")
                .setCyclic(false)
                .setMinMillseconds(System.currentTimeMillis() - tenYears)
                .setMaxMillseconds(System.currentTimeMillis() + tenYears)
                .setCurrentMillseconds(System.currentTimeMillis())
                .setThemeColor(getResources().getColor(R.color.colorPrimary))
                .setType(Type.YEAR_MONTH_DAY)
                .setWheelItemTextNormalColor(getResources().getColor(R.color.timetimepicker_default_text_color))
                .setWheelItemTextSelectorColor(getResources().getColor(R.color.colorPrimary))
                .setWheelItemTextSize(12)
                .build();


        dates.add("今天");
        dates.add("一周内");
        dates.add("一个月");
        dates.add("三个月");
        dates.add("半年");
        dates.add("一年内");
        ChooseTimeAdapter adapter = new ChooseTimeAdapter(mContext);
        adapter.setDataList(dates);
        GridSpacingItemDecoration decoration = new GridSpacingItemDecoration(4, WindowsUtils.dip2px(10), true);
        timeRecycler.addItemDecoration(decoration);
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 4);
        timeRecycler.setLayoutManager(layoutManager);
        timeRecycler.setAdapter(adapter);


    }

    @OnClick(R.id.time_iv_from)
    void clickChooseTimeFrom() {
        curType = TYPE_FROM;
        mDialogAll.show(getSupportFragmentManager(), "all");
    }

    @OnClick(R.id.time_iv_to)
    void clickChooseTimeTo() {
        curType = TYPE_TO;
        mDialogAll.show(getSupportFragmentManager(), "all");
    }


    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        if (curType == TYPE_FROM) {
            dateFrom = CalendarUtil.timeFormat(millseconds);
            timeTvFrom.setText(dateFrom);
        } else {
            dateEnd = CalendarUtil.timeFormat(millseconds);
            timeTvTo.setText(dateEnd);
        }

        if (!TextUtils.isEmpty(dateFrom) && !TextUtils.isEmpty(dateEnd)) {
            Intent data = new Intent();
            data.putExtra("dateStart", dateFrom);
            data.putExtra("dateEnd", dateEnd);
            setResult(0, data);
            finish();
        }

    }


    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(-1,R.anim.slide_out_from_bottom);
    }
}
